{include file="index/_meta" /}
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-input-inline">
            <input id="edt-search" type="text" placeholder="输入关键字" class="layui-input"/>
            </div>
        </div>
        <div class="layui-card-body">
            <div class="layui-btn-group" style="padding-bottom: 10px;">
                <button class="layui-btn layuiadmin-btn-role" data-type="add">添加</button>
                <button class="layui-btn layuiadmin-btn-role" data-type="expandAll">全部展开</button>
                <button class="layui-btn layuiadmin-btn-role" data-type="foldAll">全部折叠</button>
                <button class="layui-btn" id="btn-search">&nbsp;&nbsp;搜索&nbsp;&nbsp;</button>
            </div>

            <table id="KT_table" lay-filter="KT_table"></table>

            <script type="text/html" id="KT_table_temp">
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="addzi">添加子菜单</a>
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
            </script>
        </div>
    </div>
</div>

<script src="__STATIC__/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '__STATIC__/module/'
    }).extend({
        treetable: 'treetable-lay/treetable'
    }).use(['treetable','table'], function () {
        var treetable = layui.treetable
            ,$ = layui.jquery
            ,table = layui.table;

        var tableRender = function () {
            // 渲染表格
            treetable.render({
                treeColIndex: 2,          // treetable新增参数
                treeSpid: 0,             // treetable新增参数
                treeIdName: 'id',       // treetable新增参数
                treePidName: 'pid',     // treetable新增参数
                treeDefaultClose: false,   // treetable新增参数
                treeLinkage: true,        // treetable新增参数
                elem: '#KT_table',
                url: "{:url('menu/index')}",
                cols: [[
                    {type: 'checkbox'},
                    {field: 'id', title: 'id'},
                    {field: 'title', title: '名称'},
                    {field: 'url', title: 'URL'},
                    {field: 'sort', title: '排序'},
                    {field: 'url', title: '操作',templet:"#KT_table_temp"},
                ]]
            });
        }
        tableRender();


        var active = {
            expandAll: function(){
                treetable.expandAll("#KT_table")
            },
            foldAll: function(){
                treetable.foldAll("#KT_table")
            },
            add:function () {
                add('添加',"add.html","",function () {
                    tableRender()
                });
            },
            batchdel:function () {
                var checkStatus = table.checkStatus('KT_table'),
                    data = checkStatus.data,
                    newsId = [];
                if(data.length > 0) {
                    for (var i in data) {
                        newsId.push(data[i].id);
                    }
                    layer.confirm('确定删除？', {icon: 3, title: '提示信息'}, function (index) {
                        $.post("{:url('menu/del')}",{
                            id : newsId
                        },function(data){
                            tableRender();
                            layer.close(index);
                        })
                    })
                }else{
                    layer.msg("请选择需要删除的数据");
                }
            }
        }
        $('.layui-btn.layuiadmin-btn-role').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        $('#btn-search').click(function () {
            var keyword = $('#edt-search').val();
            var searchCount = 0;
            $('#KT_table').next('.treeTable').find('.layui-table-body tbody tr td:nth-child(3)').each(function () {
                $(this).css('background-color', 'transparent');
                var text = $(this).text();
                if (keyword != '' && text.indexOf(keyword) >= 0) {
                    $(this).css('background-color', 'rgba(250,230,160,0.5)');
                    if (searchCount == 0) {
                        treetable.expandAll('#KT_table');
                        $('html,body').stop(true);
                        $('html,body').animate({scrollTop: $(this).offset().top - 150}, 500);
                    }
                    searchCount++;
                }
            });
            if (keyword == '') {
                layer.msg("请输入搜索内容", {icon: 5});
            } else if (searchCount == 0) {
                layer.msg("没有匹配结果", {icon: 5});
            }
        });

        table.on('tool(KT_table)', function(obj){
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr;

            if(layEvent === 'del'){ //删除
                layer.confirm('真的删除行么', function(index){

                    $.post("{:url('menu/del')}",{
                        id : data.id
                    },function(data){
                        tableRender();
                        layer.close(index);
                    })
                    obj.del();
                    layer.close(index);

                });
            } else if(layEvent === 'edit'){ //编辑
                add("编辑","edit.html?id="+data.id,"",function () {
                    tableRender();
                })
            } else if(layEvent === 'addzi'){
                add("添加","add.html?id="+data.id,"",function () {
                    tableRender();
                })
            }
        });
    });
</script>
</body>
</html>

